﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace August.UI
{
    public partial class ShowContainer : UserControlX
    {
        public static int TouchRadius = 6;
        Point mousedownP;
        bool ismousedown = false;
        public ShowContainer()
        {
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            ismousedown = true;
            mousedownP = e.Location;
            if (ShapeItems.Count != 0)
            {
                for (int i = 0; i < ShapeItems.Count; i++)
                {
                    int k = ShapeItems.Count - i - 1;
                    ShapeItems[k].m_DrawColor = ShapeItems[k].DrawColor;
                    ShapeItems[k].DrawLineWidth = ShapeItems[k].DrawLineWidth;
                }
                for (int i = 0; i < ShapeItems.Count; i++)
                {
                   
                    int k = ShapeItems.Count - i - 1;
                    ShapeItems[k].m_mouseDownPointF = mousedownP;
                    if (ShapeItems[k].OnDownPaint(e))
                        break;
                    //if (ShapeItems[k].DrawGraphicsPath.IsOutlineVisible(e.X, e.Y, new Pen(Color.Empty, ShapeItems[k].DrawLineWidth + TouchRadius)))
                    //{
                    //    ShapeItems[k].m_isSelected = true;
                    //    ShapeItems[k].m_DrawColor = ShapeBase.ColorMouseDown;
                    //    ShapeItems[k].DrawLineWidth = ShapeItems[k].DrawLineWidth;
                    //    ShapeItems[k].m_mouseDownMovePointFs = new PointF[ShapeItems[k].PointFs.Length];
                    //    ShapeItems[k].PointFs.CopyTo(ShapeItems[k].m_mouseDownMovePointFs, 0);
                    //    int index = -1;
                    //    ShapeItems[k].m_IsSelectedPoint = IsHitPoint(e.X, e.Y, ShapeItems[k].LastPointFs, ref index);
                    //    ShapeItems[k].m_selectedPointIndex = index;
                    //    break;
                    //}
                }
                Refresh();
            }
            base.OnMouseDown(e);
        }

        protected bool IsHitPoint(int x, int y, PointF[] pointFs, ref int index)
        {
            index = -1;
            for (int k = 0; k < pointFs.Length; k++)
            {
                if (Math.Abs(pointFs[k].X - x) < TouchRadius && Math.Abs(pointFs[k].Y - y) < TouchRadius)
                {
                    index = k;
                    return true;
                }
            }
            return false;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (ismousedown)
            {
                for (int i = 0; i < ShapeItems.Count; i++)
                {
                    ShapeItems[i].OnMovePaint(e);
                    Refresh();
                }
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            ismousedown = false;
            for (int i = 0; i < ShapeItems.Count; i++)
            {
                if (ShapeItems[i].m_isSelected)
                {
                    ShapeItems[i].m_isSelected = false;
                }
            }
            base.OnMouseUp(e);
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //try
            //{
                base.OnPaintBackground(e);
                e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                for (int i = 0; i < ImageItems.Count; i++)
                {
                    ImageItems[i].OnWheelPaint(e);
                }
            //}
            //catch (Exception exp)
            //{
            //    FastData.SaveStatic.SaveTxt("D:\\ERROR.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\tOnPaintBackground:" + exp.ToString(), true);
            //}
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                for (int i = 0; i < ShapeItems.Count; i++)
                {
                    ShapeItems[i].OnWheelPaint(e);
                }
            }
            catch (Exception exp)
            {
                //FastData.SaveStatic.SaveTxt("D:\\ERROR.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\tOnPaint:" + exp.ToString(), true);
            }

        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ShowContainer
            // 
            this.BackColor = System.Drawing.Color.DarkSalmon;
            this.Name = "ShowContainer";
            this.Size = new System.Drawing.Size(98, 87);
            this.ResumeLayout(false);

        }

        //private void InitializeComponent()
        //{
        //    this.SuspendLayout();
        //    // 
        //    // ShowContainer
        //    // 
        //    this.Name = "ShowContainer";
        //    this.Size = new System.Drawing.Size(65535, 45709);
        //    this.ResumeLayout(false);

        //}
    }
}
